﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;             // required for Message
using System.Runtime.InteropServices;   // required for Marshal
using System.IO;
using Microsoft.Win32.SafeHandles;
using System.Drawing;
using System.Threading;
using Synch;
using System.Xml.Serialization;
using System.Xml;
// DriveDetector - rev. 1, Oct. 31 2007

/// <summary>
/// Hidden Form which we use to receive Windows messages about flash drives
/// </summary>
internal class DetectorForm : Form
{
    private Label label1;
    private NotifyIcon notifyIcon1;
    private System.ComponentModel.IContainer components;
    private ContextMenuStrip contextMenuStrip1;
    private ToolStripMenuItem toolStripMenuItem1;
    private ToolStripMenuItem toolStripMenuItem2;
  

    /// <summary>
    /// Set up the hidden form. 
    /// </summary>
    /// <param name="detector">DriveDetector object which will receive notification about USB drives, see WndProc</param>
    public DetectorForm(DriveDetector detector)
    {
//        mDetector = detector;
        this.MinimizeBox = false;
        this.MaximizeBox = false;
        this.ShowInTaskbar = false;
        this.ShowIcon = false;
        this.FormBorderStyle = FormBorderStyle.None;
        this.Load += new System.EventHandler(this.Load_Form);
        this.Activated += new EventHandler(this.Form_Activated);
    }

    private void Load_Form(object sender, EventArgs e)
    {
        // We don't really need this, just to display the label in designer ...
        InitializeComponent();

        // Create really small form, invisible anyway.
        this.Size = new System.Drawing.Size(5, 5);
    }

    private void Form_Activated(object sender, EventArgs e)
    {
        this.Visible = false;
    }

    

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DetectorForm));
        this.label1 = new System.Windows.Forms.Label();
        this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
        this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
        this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
        this.toolStripMenuItem2 = new System.Windows.Forms.ToolStripMenuItem();
        this.contextMenuStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(13, 30);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(314, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "This is invisible form. To see DriveDetector code click View Code";
        this.label1.Click += new System.EventHandler(this.label1_Click);
        // 
        // notifyIcon1
        // 
        this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
        this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
        this.notifyIcon1.Text = "notifyIcon1";
        this.notifyIcon1.Visible = true;
        this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
        this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
        // 
        // contextMenuStrip1
        // 
        this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripMenuItem1,
            this.toolStripMenuItem2});
        this.contextMenuStrip1.Name = "contextMenuStrip1";
        this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional;
        this.contextMenuStrip1.Size = new System.Drawing.Size(193, 48);
        this.contextMenuStrip1.Text = "Add a drive";
        this.contextMenuStrip1.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip1_Opening);
        // 
        // toolStripMenuItem1
        // 
        this.toolStripMenuItem1.Name = "toolStripMenuItem1";
        this.toolStripMenuItem1.Size = new System.Drawing.Size(192, 22);
        this.toolStripMenuItem1.Text = "Add a new USB device";
        this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
        // 
        // toolStripMenuItem2
        // 
        this.toolStripMenuItem2.Name = "toolStripMenuItem2";
        this.toolStripMenuItem2.Size = new System.Drawing.Size(192, 22);
        this.toolStripMenuItem2.Text = "Force synchronise";
        // 
        // DetectorForm
        // 
        this.ClientSize = new System.Drawing.Size(360, 80);
        this.Controls.Add(this.label1);
        this.Name = "DetectorForm";
        this.Load += new System.EventHandler(this.DetectorForm_Load);
        this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DetectorForm_FormClosing);
        this.contextMenuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    private void DetectorForm_Load(object sender, EventArgs e)
    {

    }

    private void notifyIcon1_Click(object sender, EventArgs e)
    {

    }

    private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

    }

    private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
    {

    }

    private void toolStripMenuItem1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog fb = new FolderBrowserDialog();
        string DeviceID = null;

        while (DeviceID == null)
        {
            if (fb.ShowDialog() == DialogResult.OK)
            {
                string DriveID = fb.SelectedPath.Substring(0, fb.SelectedPath.IndexOf(":"));
                USBSerialNumber usb = new USBSerialNumber();
                DeviceID = usb.getSerialNumberFromDriveLetter(DriveID);
                if (DeviceID == null)
                {
                    MessageBox.Show("This is not a USB drive.");
                }
                else
                {

                    MessageBox.Show("Ati ales sa adaugati USB Device-ul cu serial number-ul: " + DeviceID);
                    mDetector.DriveAddedRaiser(new Device(DeviceID, ""));
                }

            }
            else return;
        }
    }

}